package com.vincent.kdp.Activity;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.vincent.kdp.PostUrl;
import com.vincent.kdp.R;
import com.vincent.kdp.js.AddConsumeOperator;
import com.vincent.kdp.ui.other.MainActivity;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by pg on 2017/1/23.
 */

public class AddConsumeActivity extends AppCompatActivity {

    @Bind(R.id.back_img)
    ImageView mBackImg;
    @Bind(R.id.toolbar_center_tv)
    TextView mToolbarCenterTv;
    @Bind(R.id.webView)
    WebView webView;
    private String type;
    private String barCode;
    private String name;
    private String price;

    private String totalWeight="";
    private String phone="";
    private String deposit="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addconsume);
        ButterKnife.bind(this);
        mToolbarCenterTv.setText("添加消费");

        getParam(getIntent());
        this.initializeWebView();

    }

    private void getParam(Intent intent) {
        this.type=intent.getStringExtra("type");
        if("vip".equals(type)){
            this.totalWeight = intent.getStringExtra("totalWeight");
            this.phone = intent.getStringExtra("phone");
            this.deposit = intent.getStringExtra("deposit");
        }
        this.barCode = intent.getStringExtra("barCode");
        this.name = intent.getStringExtra("name");
        this.price = intent.getStringExtra("price");
    }

    @SuppressLint({"NewApi", "SetJavaScriptEnabled"})
    private void initializeWebView() {
        Log.d("AddConsumeActivity", name + "----" + barCode + "----" + price+"totalWeight--"+totalWeight+"phone--"+totalWeight+"deposit--0"+deposit);

        webView.addJavascriptInterface(new AddConsumeOperator(AddConsumeActivity.this, type,barCode,
                        name, price,totalWeight,phone,deposit),
                "Consume");
        try {
            //String url = "file:///android_asset/LoginJs/login.html";
            String url = PostUrl.SERVICE_URL + "/H5/addConsume/addConsume.html";
            WebSettings webSettings = webView.getSettings();
            webSettings.setJavaScriptEnabled(true);
            webSettings.setAllowFileAccess(true);
            webSettings.setAllowFileAccessFromFileURLs(true);
            webSettings.setBuiltInZoomControls(false);
            webSettings.setSupportZoom(false);
            webSettings.setDisplayZoomControls(false);
            setClient();
            this.webView.loadUrl(url);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @OnClick({R.id.back_img})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_img:
                this. finish();
                overridePendingTransition(com.vincent.baseapp.R.anim.fragment_left_in, com.vincent.baseapp.R.anim.fragment_right_out);
                break;
        }
    }

    private void setClient() {
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
                AlertDialog.Builder b2 = new AlertDialog.Builder(AddConsumeActivity.this).setTitle("通知")
                        .setMessage(message).setPositiveButton("确定", new AlertDialog.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                result.confirm();
                            }
                        });
                b2.setCancelable(false);
                b2.create();
                b2.show();
                return true;
            }
        });
    }


}
